﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using AlarmManagePlatform.Model.DB;

namespace AlarmManagePlatform.DataAccess
{
    public class tb_ParaInfo
    {
        internal List<Model.DB.tb_ParaInfo> getAll()
        {
            Relations.DBUtility.SqlServer.SqlDb sqlDb = null;
            try
            {
                List<Model.DB.tb_ParaInfo> result = null;
                Model.DB.tb_ParaInfo tb_ParaInfo;
                DataTable dataTable;
                string sqlScript = "select Id,Code,[Desc],Unit,RangeMin,RangeMax,ConversionFactor,OrderId from tb_ParaInfo order by Id;";
                sqlDb = Static.Methods.OpenDataBase();
                dataTable = sqlDb.RunSqlScript(sqlScript);
                result = new List<Model.DB.tb_ParaInfo>();
                for (int i = 0; i < dataTable.Rows.Count; i++)
                {
                    tb_ParaInfo = new Model.DB.tb_ParaInfo();
                    tb_ParaInfo.Id = int.Parse(dataTable.Rows[i]["Id"].ToString());
                    tb_ParaInfo.Code = dataTable.Rows[i]["Code"].ToString();
                    tb_ParaInfo.Desc = dataTable.Rows[i]["Desc"].ToString();
                    tb_ParaInfo.Unit = dataTable.Rows[i]["Unit"].ToString();
                    tb_ParaInfo.RangeMin = float.Parse(dataTable.Rows[i]["RangeMin"].ToString());
                    tb_ParaInfo.RangeMax = float.Parse(dataTable.Rows[i]["RangeMax"].ToString());
                    tb_ParaInfo.ConversionFactor = float.Parse(dataTable.Rows[i]["ConversionFactor"].ToString());
                    tb_ParaInfo.OrderId = int.Parse(dataTable.Rows[i]["OrderId"].ToString());
                    result.Add(tb_ParaInfo);
                }
                return result;
            }
            catch (Exception ex)
            {
                throw new Exception("获取参数信息失败，错误信息：" + ex.Message);
            }
            finally
            {
                if (sqlDb != null)
                    Static.Methods.CloseDataBase(sqlDb);
            }
        }

        internal Model.DB.tb_ParaInfo GetParaInfo(string id)
        {
            Relations.DBUtility.SqlServer.SqlDb _SqlDb = null;
            Model.DB.tb_ParaInfo _Result = null;
            DataTable _DataTable;
            string _SqlScript;
            try
            {
                _SqlScript = $"SElECT * From tb_ParaInfo Where Id = {id}";
                _SqlDb = Static.Methods.OpenDataBase();
                _DataTable = _SqlDb.RunSqlScript(_SqlScript);

                if (_DataTable.Rows.Count > 0)
                {
                    _Result = new Model.DB.tb_ParaInfo();
                    _Result.Id = Convert.ToInt32(_DataTable.Rows[0]["Id"].ToString());
                    _Result.Code = _DataTable.Rows[0]["Code"].ToString();
                    _Result.Desc = _DataTable.Rows[0]["Desc"].ToString();
                    _Result.Unit = _DataTable.Rows[0]["Unit"].ToString();
                    if (!string.IsNullOrEmpty(_DataTable.Rows[0]["RangeMax"].ToString()))
                        _Result.RangeMax = Convert.ToSingle(_DataTable.Rows[0]["RangeMax"].ToString());
                    else
                        _Result.RangeMax = 100;

                    if (!string.IsNullOrEmpty(_DataTable.Rows[0]["RangeMin"].ToString()))
                        _Result.RangeMin = Convert.ToSingle(_DataTable.Rows[0]["RangeMin"].ToString());
                    else
                        _Result.RangeMin = 0;
                    _Result.ConversionFactor = float.Parse(_DataTable.Rows[0]["ConversionFactor"].ToString());
                    _Result.OrderId = Convert.ToInt32(_DataTable.Rows[0]["OrderId"].ToString());
                }
                return _Result;
            }
            catch (Exception ex)
            {
                throw new Exception("获取参数细信息失败，错误信息：" + ex.Message);
            }
            finally
            {
                if (_SqlDb != null)
                    Static.Methods.CloseDataBase(_SqlDb);
            }
        }
    }
}